﻿using ITSM.Model;
using ITSM.Model.Model;
using ITSM.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace ITSM.Controllers.Demand
{
    [Route("api/[controller]")]
    [ApiController]
    public class DemandController : ControllerBase
    {
        private readonly IDemand _demand;
        public DemandController(IDemand demand)
        {
            _demand = demand;
        }

        // GET api/<DemandController>/5
        [HttpPost("/api/DemandList")]
        public IActionResult PostList(M_DemandList demand)
        {
            if (string.IsNullOrWhiteSpace(demand.RYD_UserId))
            {
                return NotFound();
            }
            if (string.IsNullOrWhiteSpace(demand.RYD_Company))
            {
                return NotFound();
            }
            if (demand.Page <= 0)
            {
                return NotFound();
            }
            return Ok(_demand.DemandList(demand));
        }

        [Route("/api/DemandNum")]
        [HttpGet()]
        public IActionResult GetNum(string RYD_Company, string RYD_UserId, string RYD_UserName, int RYD_DemandId)
        {
            if (string.IsNullOrWhiteSpace(RYD_UserId))
            {
                return NotFound();
            }
            if (string.IsNullOrWhiteSpace(RYD_Company))
            {
                return NotFound();
            }
            M_DemandList demandList = new()
            {
                RYD_Company = RYD_Company,
                RYD_UserId = RYD_UserId,
                RYD_UserName = RYD_UserName,
                RYD_DemandId = RYD_DemandId
            };
            return Ok(_demand.DemandNum(demandList));
        }
        [Route("/api/OtherDemandNum")]
        [HttpGet()]
        public IActionResult GetOtherCompanyDemandNum(string RYD_Company, string RYD_UserId)
        {
            if (string.IsNullOrWhiteSpace(RYD_UserId))
            {
                return NotFound();
            }
            if (string.IsNullOrWhiteSpace(RYD_Company))
            {
                return NotFound();
            }
            M_DemandList demandList = new()
            {
                RYD_Company = RYD_Company,
                RYD_UserId = RYD_UserId
            };
            return Ok(_demand.OtherCompanyDemandNum(demandList));
        }

        [Route("/api/DemandInfo")]
        [HttpGet()]
        public IActionResult GetInfo(int RYD_DemandId)
        {
            if (RYD_DemandId == 0)
            {
                return NotFound();
            }
            return Ok(_demand.DemandInfo(RYD_DemandId));
        }
        [Route("/api/GetLastDemand")]
        [HttpGet()]
        public IActionResult GetLastDemand(string RYD_Id)
        {
            if (string.IsNullOrWhiteSpace(RYD_Id))
            {
                return NotFound();
            }
            var data = _demand.QueryLastDemand(RYD_Id);
            if (data == null)
            {
                return Ok(null);
            }
            M_DemandInfo m_DemandInfo = new()
            {
                RYD_UserName = data.RYD_UserName,
                RYD_UserPhone = data.RYD_UserPhone,
                RYD_UserAddress = data.RYD_UserAddress,
                RYD_UserIp = data.RYD_UserIp
            };
            return Ok(m_DemandInfo);
        }

        // POST api/<DemandController>
        [HttpPost]
        public IActionResult Post(M_Demand demand)
        {
            if (string.IsNullOrWhiteSpace(demand.RYD_Company) || string.IsNullOrWhiteSpace(demand.RYD_UserId) || demand.RYD_TypeId == 0 ||
                string.IsNullOrWhiteSpace(demand.RYD_DemandDetails) || string.IsNullOrWhiteSpace(demand.RYD_UserName) || string.IsNullOrWhiteSpace(demand.RYD_UserPhone) ||
                string.IsNullOrWhiteSpace(demand.RYD_UserAddress))
            {
                return NotFound();
            }

            Image image = new();
            Random random = new();
            string imageUrl = "";

            foreach (var item in demand.RYD_FilesT)
            {
                string imageName = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds().ToString() + random.Next(0, 10);
                string retrunUrl = image.SaveImage(item.content, imageName + ".jpeg") + "|";
                if (string.IsNullOrWhiteSpace(retrunUrl))
                {
                    return NotFound();
                }
                imageUrl += retrunUrl;

            }

            if (imageUrl.Length > 0)
            {
                if (imageUrl[^1] == '|')
                {
                    imageUrl = imageUrl[..^1];
                }
            }
            demand.RYD_Files = imageUrl;
            string userId = _demand.AddDemand(demand);
            if (string.IsNullOrWhiteSpace(userId))
            {
                return NotFound();
            }
            return Ok(userId);
        }
        [HttpPost("/api/Demand2")]
        public IActionResult Post2(M_Demand demand)
        {
            if (string.IsNullOrWhiteSpace(demand.RYD_Company) || string.IsNullOrWhiteSpace(demand.RYD_UserId) || demand.RYD_TypeId == 0 ||
                string.IsNullOrWhiteSpace(demand.RYD_DemandDetails) || string.IsNullOrWhiteSpace(demand.RYD_UserName) || string.IsNullOrWhiteSpace(demand.RYD_UserPhone) ||
                string.IsNullOrWhiteSpace(demand.RYD_UserAddress) || demand.RYD_DemandId == 0)
            {
                return NotFound();
            }

            Image image = new();
            Random random = new();
            string imageUrl = "";

            foreach (var item in demand.RYD_FilesT)
            {
                if (string.IsNullOrWhiteSpace(item.url))
                {
                    string imageName = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds().ToString() + random.Next(0, 10);
                    string retrunUrl = image.SaveImage(item.content, imageName + ".jpeg") + "|";
                    if (string.IsNullOrWhiteSpace(retrunUrl))
                    {
                        return NotFound();
                    }
                    imageUrl += retrunUrl;
                }
                else
                {
                    imageUrl += item.url + "|";
                }
            }

            if (imageUrl.Length > 0)
            {
                if (imageUrl[^1] == '|')
                {
                    imageUrl = imageUrl[..^1];
                }
            }
            demand.RYD_Files = imageUrl;
            string userId = _demand.AddDemand2(demand);
            if (string.IsNullOrWhiteSpace(userId))
            {
                return NotFound();
            }
            return Ok(userId);
        }

        // POST api/<DemandController>
        [HttpPost("/api/RecordDemand")]
        public IActionResult RecordDemand(M_Demand demand)
        {
            if (string.IsNullOrWhiteSpace(demand.RYD_Company) || string.IsNullOrWhiteSpace(demand.RYD_UserId) || demand.RYD_TypeId == 0 ||
                string.IsNullOrWhiteSpace(demand.RYD_DemandDetails) || string.IsNullOrWhiteSpace(demand.RYD_Solution) || string.IsNullOrWhiteSpace(demand.RYD_Reason))
            {
                return NotFound();
            }

            Image image = new();
            Random random = new();
            string imageUrl = "";

            foreach (var item in demand.RYD_FilesT)
            {
                string imageName = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds().ToString() + random.Next(0, 10);
                string retrunUrl = image.SaveImage(item.content, imageName + ".jpeg") + "|";
                if (string.IsNullOrWhiteSpace(retrunUrl))
                {
                    return NotFound();
                }
                imageUrl += retrunUrl;

            }

            if (imageUrl.Length > 0)
            {
                if (imageUrl[^1] == '|')
                {
                    imageUrl = imageUrl[..^1];
                }
            }
            demand.RYD_Files = imageUrl;
            if (_demand.RecordDemand(demand))
            {
                return Ok(true);
            }
            return NotFound();
        }

        [HttpPost("/api/AddDemand")]
        public IActionResult PostW(M_Demand demand)
        {
            if (string.IsNullOrWhiteSpace(demand.RYD_Company) || string.IsNullOrWhiteSpace(demand.RYD_UserId) || demand.RYD_TypeId == 0 ||
                string.IsNullOrWhiteSpace(demand.RYD_DemandDetails) || string.IsNullOrWhiteSpace(demand.RYD_UserName) || string.IsNullOrWhiteSpace(demand.RYD_UserPhone) ||
                string.IsNullOrWhiteSpace(demand.RYD_UserAddress))
            {
                return NotFound();
            }
            if (demand.RYD_Files.Length > 0)
            {
                if (demand.RYD_Files[^1] == '|')
                {
                    demand.RYD_Files = demand.RYD_Files[..^1];
                }
            }
            string userId = _demand.AddDemand(demand);
            if (string.IsNullOrWhiteSpace(userId))
            {
                return NotFound();
            }
            return Ok(userId);
        }
        [HttpPost("/api/AddDemand2")]
        public IActionResult PostW2(M_Demand demand)
        {
            if (string.IsNullOrWhiteSpace(demand.RYD_Company) || string.IsNullOrWhiteSpace(demand.RYD_UserId) || demand.RYD_TypeId == 0 ||
                string.IsNullOrWhiteSpace(demand.RYD_DemandDetails) || string.IsNullOrWhiteSpace(demand.RYD_UserName) || string.IsNullOrWhiteSpace(demand.RYD_UserPhone) ||
                string.IsNullOrWhiteSpace(demand.RYD_UserAddress) || demand.RYD_DemandId == 0)
            {
                return NotFound();
            }
            if (demand.RYD_Files.Length > 0)
            {
                if (demand.RYD_Files[^1] == '|')
                {
                    demand.RYD_Files = demand.RYD_Files[..^1];
                }
            }
            string userId = _demand.AddDemand2(demand);
            if (string.IsNullOrWhiteSpace(userId))
            {
                return NotFound();
            }
            return Ok(userId);
        }
        [HttpPost("/api/RecordDemandW")]
        public IActionResult RecordDemandW(M_Demand demand)
        {
            if (string.IsNullOrWhiteSpace(demand.RYD_Company) || string.IsNullOrWhiteSpace(demand.RYD_UserId) || demand.RYD_TypeId == 0 ||
                string.IsNullOrWhiteSpace(demand.RYD_DemandDetails) || string.IsNullOrWhiteSpace(demand.RYD_Solution) || string.IsNullOrWhiteSpace(demand.RYD_Reason))
            {
                return NotFound();
            }
            if (demand.RYD_Files.Length > 0)
            {
                if (demand.RYD_Files[^1] == '|')
                {
                    demand.RYD_Files = demand.RYD_Files[..^1];
                }
            }
            if (_demand.RecordDemand(demand))
            {
                return Ok(true);
            }
            return NotFound();
        }
        [HttpPost("/api/UpImage")]
        public IActionResult ImagePost()
        {
            try
            {
                //Hashtable hash = new Hashtable();
                IFormFileCollection cols = Request.Form.Files;
                if (cols == null || cols.Count == 0)
                {
                    return NotFound(false);
                }
                Random random = new();
                string imageUrl = "";
                foreach (IFormFile file in cols)
                {
                    string imageName = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds().ToString() + random.Next(0, 10);
                    Image image = new();
                    string retrunUrl = image.SaveImageBlob(file, imageName);
                    if (string.IsNullOrWhiteSpace(retrunUrl))
                    {
                        return NotFound();
                    }
                    imageUrl += retrunUrl;
                }
                return Ok(imageUrl);
            }
            catch (Exception)
            {
                return NotFound(false);
            }
        }


        [HttpPost("/api/EditorUpImage")]
        public IActionResult EditorImagePost()
        {
            try
            {
                //Hashtable hash = new Hashtable();
                IFormFileCollection cols = Request.Form.Files;
                if (cols == null || cols.Count == 0)
                {
                    return NotFound(false);
                }
                Random random = new();
                string imageUrl = "";
                foreach (IFormFile file in cols)
                {
                    string imageName = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds().ToString() + random.Next(0, 10);
                    Image image = new();
                    string retrunUrl = image.SaveImageBlob(file, imageName);
                    if (string.IsNullOrWhiteSpace(retrunUrl))
                    {
                        return NotFound();
                    }
                    imageUrl += retrunUrl;
                }
                return Ok(new
                {
                    errno = 0, // 注意：值是数字，不能是字符串
                    data = new
                    {
                        url = imageUrl, // 图片 src ，必须
                    }
                });
            }
            catch (Exception)
            {
                return NotFound(new
                {
                    errno = 1,  // 只要不等于 0 就行
                    message = "上传错误"
                });
            }
        }


    }
}


